/* @(#)GSManufactureCategoryDAO.java    
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li     2006-2-13    新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.dao;

import java.sql.Connection;
import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 
 */
public class GSManufactureCategoryDAO extends GSBaseDAO {
    /**
     * 根据产品种类等级查询产品种类信息
     * @param aLevel                产品种类等级
     * @return
     * @throws GSException
     */
    public ArrayList queryManufactureByLevel(String aLevel) throws GSException {
        String tSql = "select * from T_PRODUCT_CATEGORY b where b.CATEGORY_LEVEL=?";
        ArrayList tList = null;
        Object[] tObj = {aLevel};
        try{
            tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    
    public boolean findManufatureCategoryById(String aCategoryId) throws GSException {
        String tSql = "select * from T_PRODUCT_CATEGORY where id=? ";

        ArrayList tList = null;
        Object[] tObj = {aCategoryId};
        try{
        	tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
            if(tList.size() > 0){
                return true;
            }else{ 
            	return false;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
    }
    
    public boolean isSubExist(String aCategoryId) throws GSException {
        String tSql = "select count(CODE) from T_PRODUCT_CATEGORY b where b.UPPER_CATEGORY=? ";
        boolean bool = false;
        Object[] tObj = {aCategoryId};
        try{
            int num = this.queryRowNum(tSql,tObj);
            if(num > 0)
                return true;
            else return false;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
    }
    /**
     * 取得产品种类名称
     * @param aCategoryId       产品种类编码
     * @return                  产品种类名称                  
     * @throws GSException
     */
    public String queryManufactureCatetoryName(String aCategoryId) throws GSException{
        String tCategoryName = "";
        String tSql = "select * from T_PRODUCT_CATEGORY b where b.CODE='" + aCategoryId + "'";
        ArrayList tList = null;
        Object[] tObj = {};
        try{
            tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
            if(tList.size() == 1){
                GSManufactureCategoryBean tGSBean = (GSManufactureCategoryBean)tList.get(0);
                tCategoryName = tGSBean.getGs_manufacture_category_name();
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tCategoryName;
    }
    
    /**
     * 在产品种类表中新增一笔记录
     * 
     * @param aGSManufactureBean
     * 
     * return boolean
     * 
     * @throws CUPSException
     */
    public boolean add(GSManufactureCategoryBean aGSManufactureCategoryBean,LoginContext loginContext) throws GSException {

        String tSql="INSERT into T_PRODUCT_CATEGORY (ID,CODE,NAME,REMARK,CATEGORY_LEVEL,CATEGORY_ID1,CATEGORY_ID2," +
         "IS_TRADEFLAT_CATEGORY,STATUS,CREATE_USER_ID,CREATE_USER,"+
         "CREATE_TIME,LAST_UPDATE_USER_ID,LAST_UPDATE_USER,LAST_UPDATE_TIME,UPPER_CATEGORY"+
         ") VALUES  ((select SYS_GUID() from dual),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

          Object[] tObject = new Object[15] ;
          tObject[0]=aGSManufactureCategoryBean.getGs_manufacture_category_id();
          tObject[1]=aGSManufactureCategoryBean.getGs_manufacture_category_name();
          tObject[2]=aGSManufactureCategoryBean.getGs_manufacture_desc();
          tObject[3]=aGSManufactureCategoryBean.getGs_manufacture_level();
          tObject[4]=aGSManufactureCategoryBean.getGs_manufacture_category_id1();
          tObject[5]=aGSManufactureCategoryBean.getGs_manufacture_category_id2();
          tObject[6]=aGSManufactureCategoryBean.getGs_is_tradeflat_manuf_category();
          tObject[7]="1";
          tObject[8]=loginContext.getLoginUserId();
          tObject[9]=loginContext.getLoginUserName();
          tObject[10]=new framework.core.DateUtil().getFullString();
          tObject[11]=loginContext.getLoginUserId();
          tObject[12]=loginContext.getLoginUserName();
          tObject[13]=new framework.core.DateUtil().getFullString();
          if(aGSManufactureCategoryBean.getGs_manufacture_category_id2()!=null){
              tObject[14]=aGSManufactureCategoryBean.getGs_manufacture_category_id2();
          }else if(aGSManufactureCategoryBean.getGs_manufacture_category_id1()!=null){
              tObject[14]=aGSManufactureCategoryBean.getGs_manufacture_category_id1();
          }else{
              tObject[14]="0";
          }
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);

        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException("");
        }

        return tResult;
    }
    
    /**
     * 产品种类是否存在
     * @param orgId
     * @return
     * @throws GSException
     */
    public boolean isExist(String aManfactureCategoryId) throws GSException{
        String tSql = "select * from T_PRODUCT_CATEGORY where CODE = ?";
        boolean tFlag = false;
        Object[] tObj = {aManfactureCategoryId};
        try{
            ArrayList tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
            if(tList.size() > 0){
                tFlag = true;
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tFlag;
    }
    
    /**
     * 通过产品种类编号和产品种类名称查询所有产品种类
     * String aCategoryId
     * String aCategoryName
     * @return ArrayList
     * @throws GSException
     */
    public ArrayList queryManufactureCatetory(String aCategoryId1,String aCategoryId2,String aCategoryName) throws GSException {
        StringBuffer tSql = new StringBuffer("select * from T_PRODUCT_CATEGORY where NAME like '%" + aCategoryName+ "%' ");
        if(!aCategoryId1.equals("0")){
        tSql.append("and (CATEGORY_ID1 like '%" + aCategoryId1+ "%' or CODE like '%" + aCategoryId1+ "%') ");
        
        }
        if(!aCategoryId2.equals("0")){
          tSql.append("and CATEGORY_ID2 like '%" + aCategoryId2+ "%'");  
        }
        tSql.append(" order by CODE");
        ArrayList tArrayList = null;
        Object[] tObject = {};
        try {
            tArrayList = executeQuery(new GSManufactureCategoryBean(), tSql.toString(), tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }
        return tArrayList;
     }
    
    /**
     * 在产品种类表中删除一笔记录
     * 
     * @param aSerialNo
     *            序列号 省代码
     */
    public boolean delete(String aCategoryId) throws GSException {
        String tSql = "delete from T_PRODUCT_CATEGORY where CODE=?";
        Object[] tObject = { aCategoryId };
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 读取产品种类详细信息
     * @param orgId
     * @return
     * @throws GSException
     */
    public GSManufactureCategoryBean getCategory(String aCategoryId) throws GSException {
        String tSql = "select * from T_PRODUCT_CATEGORY where CODE = ?";
        ArrayList tList = null;
        GSManufactureCategoryBean tGSManufactureCategoryBean = null;
        Object[] tObj = {aCategoryId};
        try{
            tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
            if(tList.size() == 1){
                tGSManufactureCategoryBean = (GSManufactureCategoryBean)tList.get(0);
            }else{
                throw new GSException("");
            }
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        
        
        return tGSManufactureCategoryBean;
    }
    
    /**
     * 在产品种类表中更新一笔记录
     * 
     * @param GSManufactureCategoryBean
     * @throws CUPSException
     * @return boolean
     */
    public boolean modify(GSManufactureCategoryBean aGSManufactureCategoryBean,LoginContext loginContext) throws GSException {

        String tSql = "UPDATE T_PRODUCT_CATEGORY SET NAME=?,REMARK=?,CATEGORY_LEVEL=?," +
                "CATEGORY_ID1=?,CATEGORY_ID2=?,LAST_UPDATE_USER_ID=?,LAST_UPDATE_USER=?,LAST_UPDATE_TIME=? WHERE CODE=?";
        Object[] tObject = new Object[9];
        tObject[0] = aGSManufactureCategoryBean.getGs_manufacture_category_name();
        tObject[1] = aGSManufactureCategoryBean.getGs_manufacture_desc();
        tObject[2] = aGSManufactureCategoryBean.getGs_manufacture_level();
        tObject[3] = aGSManufactureCategoryBean.getGs_manufacture_category_id1();
        tObject[4] = aGSManufactureCategoryBean.getGs_manufacture_category_id2();
        tObject[5] = loginContext.getLoginUserId();
        tObject[6] = loginContext.getLoginUserName();
        tObject[7] = DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
        tObject[8] = aGSManufactureCategoryBean.getGs_manufacture_category_id();
       
        boolean tResult = false;
        try {
            tResult = executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
    
    /**
     * 根据产品种类等级查询产品种类信息
     * @param aLevel                产品种类等级
     * @return
     * @throws GSException
     */
    public ArrayList queryManufactureByLevel() throws GSException {
        String tSql = "select * from T_PRODUCT_CATEGORY where CATEGORY_LEVEL='1' or CATEGORY_LEVEL='2'";
        ArrayList tList = null;
        Object[] tObj = {};
        try{
            tList = this.executeQuery(new GSManufactureCategoryBean(),tSql,tObj);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
    /**
     * 查询子产品种类
     * @param aCategoryId
     * @param aLevel
     * @param aConn
     * @return
     * @throws GSException
     */
    public ArrayList querySubManufCategory(String aCategoryId,String aLevel,Connection aConn)throws GSException{
        String tSql = "select * from T_PRODUCT_CATEGORY where UPPER_CATEGORY=? and CATEGORY_LEVEL=?";
        ArrayList tList = new ArrayList();
        Object[] tObj = new Object[2];
        tObj[0] = aCategoryId;
        tObj[1] = aLevel;
        try{
            tList = this.executeQuery(aConn,new GSManufactureCategoryBean(),tSql,tObj);
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
        
    }
    
    /**
     * 通过产品种类编号货产品种类名称查询所有产品种类
     * String aCategoryId
     * 
     * @return GSManufactureCategoryBean
     * @throws GSException
     */
    public GSManufactureCategoryBean queryManufactureCatetoryByIdOrName(String tVal) throws GSException {
        StringBuffer tSql = new StringBuffer("select * from T_PRODUCT_CATEGORY where NAME=? or CODE=?");
        GSManufactureCategoryBean tGSManufactureCategoryBean=null;
        Object[] tobj={tVal,tVal};
        ArrayList tArrayList = null;
        try {
            tArrayList = this.executeQuery(new GSManufactureCategoryBean(), tSql.toString(), tobj);
            if (tArrayList!=null&&tArrayList.size()==1) {
                tGSManufactureCategoryBean=(GSManufactureCategoryBean) tArrayList.get(0);
            }
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.toString());
            throw new GSException(ex.getMessage());
        }
        return tGSManufactureCategoryBean;
     }
}
